package com.wtwd.campus.h5_manager.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wtwd.campus.h5_manager.common.PageDataVO;
import com.wtwd.campus.h5_manager.common.RespCode;
import com.wtwd.campus.h5_manager.common.RespEntity;
import com.wtwd.campus.h5_manager.entity.DeviceLog;
import com.wtwd.campus.h5_manager.entity.MessageTypeListVo;
import com.wtwd.campus.h5_manager.service.DeviceLogService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author mjy
 * @since 2022-03-14
 */
@Api(tags = "设备日志")
@RestController
@RequestMapping("/webH5-manage-device-log")
public class H5DeviceLogController {


    private static final Logger logger = LoggerFactory.getLogger(H5DeviceLogController.class);

    @Autowired
    private DeviceLogService deviceLogService;

    /**
     * 根据时间(起始时间，imei,指令类型分页查询)
     */
    @ApiOperation(value = "查询设备日志")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "currentPage", value = "页数", required = true, dataTypeClass = Integer.class)
            , @ApiImplicitParam(name = "pageSize", value = "页大小", required = true, dataTypeClass = String.class)
            , @ApiImplicitParam(name = "messageType", value = "消息类型", required = false, dataTypeClass = String.class)
            , @ApiImplicitParam(name = "type", value = "指令类型", required = false, dataTypeClass = String.class)
            , @ApiImplicitParam(name = "startTime", value = "开始时间戳", dataType = "long", required = false)
            , @ApiImplicitParam(name = "endTime", value = "结束时间戳", dataType = "long", required = false)
            , @ApiImplicitParam(name = "imei", value = "imei", dataType = "long")
    })
    @GetMapping("/queryDeviceLog")
    public RespEntity<PageDataVO<DeviceLog>> queryDeviceLog(@RequestParam(defaultValue = "1") Integer currentPage,
                                                            @RequestParam(defaultValue = "10") Integer pageSize,
                                                            @RequestParam(value = "startTime", required = false) Long startTime,
                                                            @RequestParam(value = "endTime", required = false) Long endTime,
                                                            @RequestParam(value = "imei", required = false) String imei,
                                                            @RequestParam(value = "messageType", required = false) String messageType,
                                                            @RequestParam(value = "type", required = false) String type
    ) {
        logger.info("queryDeviceLog currentPage={},pageSize={},startTime={},endTime={},imei={}", currentPage, pageSize, startTime, endTime, imei);
        if (StringUtils.isBlank(imei)) {
            return new RespEntity<>(RespCode.NO_PARAM);
        }
        Page<DeviceLog> page = new Page<>(currentPage, pageSize);
        IPage<DeviceLog> deviceLogs = deviceLogService.queryDeviceLogConditions(startTime, endTime, imei, messageType, type, page);
        return RespEntity.success(deviceLogs);
    }


    /**
     * 添加日志
     *
     * @param deviceLog
     * @return
     */
    @ApiOperation(value = "添加日志")
    @PostMapping("/addDeviceLog")
    public RespEntity addDeviceLog(@RequestBody DeviceLog deviceLog) {
//        logger.info("addDeviceLog deviceLog={}", deviceLog.toString());
        Integer rest = deviceLogService.addDeviceLog(deviceLog);
        return new RespEntity(rest > 0 ? RespCode.SUCCESS : RespCode.FAILED);
    }

    /**
     * 查询设备消息类型
     */
    @ApiOperation(value = "查询设备消息类型")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "type", value = "messageType,type", required = true, dataTypeClass = Integer.class)
    })
    @GetMapping("/getDeviceMessageType/{type}")
    public RespEntity getDeviceMessageType(@PathVariable("type") String type) {
        logger.info("getDeviceMessageType");
        MessageTypeListVo deviceMap = deviceLogService.getDeviceMessageType(type);
        return new RespEntity(RespCode.SUCCESS, deviceMap);
    }




}

